home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / include / libpurple / util.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-05-04  |  38.0 KB  |  1,146 lines

  1. /**
  2.  * @file util.h Utility Functions
  3.  * @ingroup core
  4.  *
  5.  * purple
  6.  *
  7.  * Purple is the legal property of its developers, whose names are too numerous
  8.  * to list here.  Please refer to the COPYRIGHT file distributed with this
  9.  * source distribution.
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  24.  *
  25.  * @todo Rename the functions so that they live somewhere in the purple
  26.  *       namespace.
  27.  */
  28. #ifndef _PURPLE_UTIL_H_
  29. #define _PURPLE_UTIL_H_
  30.  
  31. #include <stdio.h>
  32.  
  33. #include "account.h"
  34. #include "xmlnode.h"
  35.  
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39.  
  40. typedef struct _PurpleUtilFetchUrlData PurpleUtilFetchUrlData;
  41.  
  42. typedef struct _PurpleMenuAction
  43. {
  44.     char *label;
  45.     PurpleCallback callback;
  46.     gpointer data;
  47.     GList *children;
  48. } PurpleMenuAction;
  49.  
  50. typedef char *(*PurpleInfoFieldFormatCallback)(const char *field, size_t len);
  51.  
  52. /**
  53.  * A key-value pair.
  54.  *
  55.  * This is used by, among other things, purple_gtk_combo* functions to pass in a
  56.  * list of key-value pairs so it can display a user-friendly value.
  57.  */
  58. typedef struct _PurpleKeyValuePair
  59. {
  60.     gchar *key;
  61.     void *value;
  62.  
  63. } PurpleKeyValuePair;
  64.  
  65. /**
  66.  * Creates a new PurpleMenuAction.
  67.  *
  68.  * @param label    The text label to display for this action.
  69.  * @param callback The function to be called when the action is used on
  70.  *                 the selected item.
  71.  * @param data     Additional data to be passed to the callback.
  72.  * @param children A GList of PurpleMenuActions to be added as a submenu
  73.  *                 of the action.
  74.  * @return The PurpleMenuAction.
  75.  */
  76. PurpleMenuAction *purple_menu_action_new(const char *label, PurpleCallback callback,
  77.                                      gpointer data, GList *children);
  78.  
  79. /**
  80.  * Frees a PurpleMenuAction
  81.  *
  82.  * @param act The PurpleMenuAction to free.
  83.  */
  84. void purple_menu_action_free(PurpleMenuAction *act);
  85.  
  86. /**************************************************************************/
  87. /** @name Base16 Functions                                                */
  88. /**************************************************************************/
  89. /*@{*/
  90.  
  91. /**
  92.  * Converts a chunk of binary data to its base-16 equivalent.
  93.  *
  94.  * @param data The data to convert.
  95.  * @param len  The length of the data.
  96.  *
  97.  * @return The base-16 string in the ASCII encoding.  Must be
  98.  *         g_free'd when no longer needed.
  99.  *
  100.  * @see purple_base16_decode()
  101.  */
  102. gchar *purple_base16_encode(const guchar *data, gsize len);
  103.  
  104. /**
  105.  * Converts an ASCII string of base-16 encoded data to
  106.  * the binary equivalent.
  107.  *
  108.  * @param str     The base-16 string to convert to raw data.
  109.  * @param ret_len The length of the returned data.  You can
  110.  *                pass in NULL if you're sure that you know
  111.  *                the length of the decoded data, or if you
  112.  *                know you'll be able to use strlen to
  113.  *                determine the length, etc.
  114.  *
  115.  * @return The raw data.  Must be g_free'd when no longer needed.
  116.  *
  117.  * @see purple_base16_encode()
  118.  */
  119. guchar *purple_base16_decode(const char *str, gsize *ret_len);
  120.  
  121. /*@}*/
  122.  
  123. /**************************************************************************/
  124. /** @name Base64 Functions                                                */
  125. /**************************************************************************/
  126. /*@{*/
  127.  
  128. /**
  129.  * Converts a chunk of binary data to its base-64 equivalent.
  130.  *
  131.  * @param data The data to convert.
  132.  * @param len  The length of the data.
  133.  *
  134.  * @return The base-64 string in the ASCII encoding.  Must be
  135.  *         g_free'd when no longer needed.
  136.  *
  137.  * @see purple_base64_decode()
  138.  */
  139. gchar *purple_base64_encode(const guchar *data, gsize len);
  140.  
  141. /**
  142.  * Converts an ASCII string of base-64 encoded data to
  143.  * the binary equivalent.
  144.  *
  145.  * @param str     The base-64 string to convert to raw data.
  146.  * @param ret_len The length of the returned data.  You can
  147.  *                pass in NULL if you're sure that you know
  148.  *                the length of the decoded data, or if you
  149.  *                know you'll be able to use strlen to
  150.  *                determine the length, etc.
  151.  *
  152.  * @return The raw data.  Must be g_free'd when no longer needed.
  153.  *
  154.  * @see purple_base64_encode()
  155.  */
  156. guchar *purple_base64_decode(const char *str, gsize *ret_len);
  157.  
  158. /*@}*/
  159.  
  160. /**************************************************************************/
  161. /** @name Quoted Printable Functions                                      */
  162. /**************************************************************************/
  163. /*@{*/
  164.  
  165. /**
  166.  * Converts a quoted printable string back to its readable equivalent.
  167.  * What is a quoted printable string, you ask?  It's an encoding used
  168.  * to transmit binary data as ASCII.  It's intended purpose is to send
  169.  * e-mails containing non-ASCII characters.  Wikipedia has a pretty good
  170.  * explanation.  Also see RFC 2045.
  171.  *
  172.  * @param str     The quoted printable ASCII string to convert to raw data.
  173.  * @param ret_len The length of the returned data.
  174.  *
  175.  * @return The readable string.  Must be g_free'd when no longer needed.
  176.  */
  177. guchar *purple_quotedp_decode(const char *str, gsize *ret_len);
  178.  
  179. /*@}*/
  180.  
  181. /**************************************************************************/
  182. /** @name MIME Functions                                                  */
  183. /**************************************************************************/
  184. /*@{*/
  185.  
  186. /**
  187.  * Converts a MIME header field string back to its readable equivalent
  188.  * according to RFC 2047.  Basically, a header is plain ASCII and can
  189.  * contain any number of sections called "encoded-words."  The format
  190.  * of an encoded word is =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=
  191.  * =? designates the beginning of the encoded-word
  192.  * ?= designates the end of the encoded-word
  193.  *
  194.  * An encoded word is segmented into three pieces by the use of a
  195.  * question mark.  The first piece is the character set, the second
  196.  * piece is the encoding, and the third piece is the encoded text.
  197.  *
  198.  * @param str The ASCII string, possibly containing any number of
  199.  *            encoded-word sections.
  200.  *
  201.  * @return The string, with any encoded-word sections decoded and
  202.  *         converted to UTF-8.  Must be g_free'd when no longer
  203.  *         needed.
  204.  */
  205. char *purple_mime_decode_field(const char *str);
  206.  
  207. /*@}*/
  208.  
  209.  
  210. /**************************************************************************/
  211. /** @name Date/Time Functions                                             */
  212. /**************************************************************************/
  213. /*@{*/
  214.  
  215. /**
  216.  * Formats a time into the specified format.
  217.  *
  218.  * This is essentially strftime(), but it has a static buffer
  219.  * and handles the UTF-8 conversion for the caller.
  220.  *
  221.  * This function also provides the GNU %z formatter if the underlying C
  222.  * library doesn't.  However, the format string parser is very naive, which
  223.  * means that conversions specifiers to %z cannot be guaranteed.  The GNU
  224.  * strftime(3) man page describes %z as: 'The time-zone as hour offset from
  225.  * GMT.  Required to emit RFC822-conformant dates
  226.  * (using "%a, %d %b %Y %H:%M:%S %z"). (GNU)'
  227.  *
  228.  * On Windows, this function also converts the results for %Z from a timezone
  229.  * name (as returned by the system strftime() %Z format string) to a timezone
  230.  * abbreviation (as is the case on Unix).  As with %z, conversion specifiers
  231.  * should not be used.
  232.  *
  233.  * @param format The format string, in UTF-8
  234.  * @param tm     The time to format, or @c NULL to use the current local time
  235.  *
  236.  * @return The formatted time, in UTF-8.
  237.  *
  238.  * @note @a format is required to be in UTF-8.  This differs from strftime(),
  239.  *       where the format is provided in the locale charset.
  240.  */
  241. const char *purple_utf8_strftime(const char *format, const struct tm *tm);
  242.  
  243. /**
  244.  * Formats a time into the user's preferred short date format.
  245.  *
  246.  * The returned string is stored in a static buffer, so the result
  247.  * should be g_strdup()'d if it's going to be kept.
  248.  *
  249.  * @param tm The time to format, or @c NULL to use the current local time
  250.  *
  251.  * @return The date, formatted as per the user's settings.
  252.  */
  253. const char *purple_date_format_short(const struct tm *tm);
  254.  
  255. /**
  256.  * Formats a time into the user's preferred short date plus time format.
  257.  *
  258.  * The returned string is stored in a static buffer, so the result
  259.  * should be g_strdup()'d if it's going to be kept.
  260.  *
  261.  * @param tm The time to format, or @c NULL to use the current local time
  262.  *
  263.  * @return The timestamp, formatted as per the user's settings.
  264.  */
  265. const char *purple_date_format_long(const struct tm *tm);
  266.  
  267. /**
  268.  * Formats a time into the user's preferred full date and time format.
  269.  *
  270.  * The returned string is stored in a static buffer, so the result
  271.  * should be g_strdup()'d if it's going to be kept.
  272.  *
  273.  * @param tm The time to format, or @c NULL to use the current local time
  274.  *
  275.  * @return The date and time, formatted as per the user's settings.
  276.  */
  277. const char *purple_date_format_full(const struct tm *tm);
  278.  
  279. /**
  280.  * Formats a time into the user's preferred time format.
  281.  *
  282.  * The returned string is stored in a static buffer, so the result
  283.  * should be g_strdup()'d if it's going to be kept.
  284.  *
  285.  * @param tm The time to format, or @c NULL to use the current local time
  286.  *
  287.  * @return The time, formatted as per the user's settings.
  288.  */
  289. const char *purple_time_format(const struct tm *tm);
  290.  
  291. /**
  292.  * Builds a time_t from the supplied information.
  293.  *
  294.  * @param year  The year.
  295.  * @param month The month.
  296.  * @param day   The day.
  297.  * @param hour  The hour.
  298.  * @param min   The minute.
  299.  * @param sec   The second.
  300.  *
  301.  * @return A time_t.
  302.  */
  303. time_t purple_time_build(int year, int month, int day, int hour,
  304.                        int min, int sec);
  305.  
  306. /** Used by purple_str_to_time to indicate no timezone offset was
  307.   * specified in the timestamp string. */
  308. #define PURPLE_NO_TZ_OFF -500000
  309.  
  310. /**
  311.  * Parses a timestamp in jabber, ISO8601, or MM/DD/YYYY format and returns
  312.  * a time_t.
  313.  *
  314.  * @param timestamp The timestamp
  315.  * @param utc       Assume UTC if no timezone specified
  316.  * @param tm        If not @c NULL, the caller can get a copy of the
  317.  *                  struct tm used to calculate the time_t return value.
  318.  * @param tz_off    If not @c NULL, the caller can get a copy of the
  319.  *                  timezone offset (from UTC) used to calculate the time_t
  320.  *                  return value. Note: Zero is a valid offset. As such,
  321.  *                  the value of the macro @c PURPLE_NO_TZ_OFF indicates no
  322.  *                  offset was specified (which means that the local
  323.  *                  timezone was used in the calculation).
  324.  * @param rest      If not @c NULL, the caller can get a pointer to the
  325.  *                  part of @a timestamp left over after parsing is
  326.  *                  completed, if it's not the end of @a timestamp.
  327.  *
  328.  * @return A time_t.
  329.  */
  330. time_t purple_str_to_time(const char *timestamp, gboolean utc,
  331.                         struct tm *tm, long *tz_off, const char **rest);
  332.  
  333. /*@}*/
  334.  
  335.  
  336. /**************************************************************************/
  337. /** @name Markup Functions                                                */
  338. /**************************************************************************/
  339. /*@{*/
  340.  
  341. /**
  342.  * Finds an HTML tag matching the given name.
  343.  *
  344.  * This locates an HTML tag's start and end, and stores its attributes
  345.  * in a GData hash table. The names of the attributes are lower-cased
  346.  * in the hash table, and the name of the tag is case insensitive.
  347.  *
  348.  * @param needle      The name of the tag
  349.  * @param haystack      The null-delimited string to search in
  350.  * @param start          A pointer to the start of the tag if found
  351.  * @param end          A pointer to the end of the tag if found
  352.  * @param attributes  The attributes, if the tag was found.  This should
  353.  *                    be freed with g_datalist_clear().
  354.  * @return TRUE if the tag was found
  355.  */
  356. gboolean purple_markup_find_tag(const char *needle, const char *haystack,
  357.                               const char **start, const char **end,
  358.                               GData **attributes);
  359.  
  360. /**
  361.  * Extracts a field of data from HTML.
  362.  *
  363.  * This is a scary function. See protocols/msn/msn.c and
  364.  * protocols/yahoo/yahoo_profile.c for example usage.
  365.  *
  366.  * @param str            The string to parse.
  367.  * @param len            The size of str.
  368.  * @param user_info      The destination PurpleNotifyUserInfo to which the new
  369.  *                       field info should be added.
  370.  * @param start_token    The beginning token.
  371.  * @param skip           The number of characters to skip after the
  372.  *                       start token.
  373.  * @param end_token      The ending token.
  374.  * @param check_value    The value that the last character must meet.
  375.  * @param no_value_token The token indicating no value is given.
  376.  * @param display_name   The short descriptive name to display for this token.
  377.  * @param is_link        TRUE if this should be a link, or FALSE otherwise.
  378.  * @param link_prefix    The prefix for the link.
  379.  * @param format_cb      A callback to format the value before adding it.
  380.  *
  381.  * @return TRUE if successful, or FALSE otherwise.
  382.  */
  383. gboolean purple_markup_extract_info_field(const char *str, int len, PurpleNotifyUserInfo *user_info,
  384.                                         const char *start_token, int skip,
  385.                                         const char *end_token, char check_value,
  386.                                         const char *no_value_token,
  387.                                         const char *display_name, gboolean is_link,
  388.                                         const char *link_prefix,
  389.                     PurpleInfoFieldFormatCallback format_cb);
  390.  
  391. /**
  392.  * Converts HTML markup to XHTML.
  393.  *
  394.  * @param html       The HTML markup.
  395.  * @param dest_xhtml The destination XHTML output.
  396.  * @param dest_plain The destination plain-text output.
  397.  */
  398. void purple_markup_html_to_xhtml(const char *html, char **dest_xhtml,
  399.                                char **dest_plain);
  400.  
  401. /**
  402.  * Strips HTML tags from a string.
  403.  *
  404.  * @param str The string to strip HTML from.
  405.  *
  406.  * @return The new string without HTML. This must be freed.
  407.  */
  408. char *purple_markup_strip_html(const char *str);
  409.  
  410. /**
  411.  * Adds the necessary HTML code to turn URIs into HTML links in a string.
  412.  *
  413.  * @param str The string to linkify.
  414.  *
  415.  * @return The linkified text.
  416.  */
  417. char *purple_markup_linkify(const char *str);
  418.  
  419. /**
  420.  * Unescapes HTML entities to their literal characters.
  421.  * For example "&" is replaced by '&' and so on.
  422.  * Actually only "&", """, "<" and ">" are currently
  423.  * supported.
  424.  *
  425.  * @param html The string in which to unescape any HTML entities
  426.  *
  427.  * @return the text with HTML entities literalized
  428.  */
  429. char *purple_unescape_html(const char *html);
  430.  
  431. /**
  432.  * Returns a newly allocated substring of the HTML UTF-8 string "str".
  433.  * The markup is preserved such that the substring will have the same
  434.  * formatting as original string, even though some tags may have been
  435.  * opened before "x", or may close after "y". All open tags are closed
  436.  * at the end of the returned string, in the proper order.
  437.  *
  438.  * Note that x and y are in character offsets, not byte offsets, and
  439.  * are offsets into an unformatted version of str. Because of this,
  440.  * this function may be sensitive to changes in GtkIMHtml and may break
  441.  * when used with other UI's. libpurple users are encouraged to report and
  442.  * work out any problems encountered.
  443.  *
  444.  * @param str The input NUL terminated, HTML, UTF-8 (or ASCII) string.
  445.  * @param x The character offset into an unformatted version of str to
  446.  *          begin at.
  447.  * @param y The character offset (into an unformatted vesion of str) of
  448.  *          one past the last character to include in the slice.
  449.  *
  450.  * @return The HTML slice of string, with all formatting retained.
  451.  */
  452. char *purple_markup_slice(const char *str, guint x, guint y);
  453.  
  454. /**
  455.  * Returns a newly allocated string containing the name of the tag
  456.  * located at "tag". Tag is expected to point to a '<', and contain
  457.  * a '>' sometime after that. If there is no '>' and the string is
  458.  * not NUL terminated, this function can be expected to segfault.
  459.  *
  460.  * @param tag The string starting a HTML tag.
  461.  * @return A string containing the name of the tag.
  462.  */
  463. char *purple_markup_get_tag_name(const char *tag);
  464.  
  465. /**
  466.  * Returns a constant string of the character representation of the HTML
  467.  * entity pointed to by @a text. For example, purple_markup_unescape_entity("&")
  468.  * will return "&". The @a text variable is expected to point to an '&',
  469.  * the first character of the entity. If given an unrecognized entity, the function
  470.  * returns @c NULL.
  471.  *
  472.  * Note that this function, unlike purple_unescape_html(), does not search
  473.  * the string for the entity, does not replace the entity, and does not
  474.  * return a newly allocated string.
  475.  *
  476.  * @param text   A string containing an HTML entity.
  477.  * @param length If not @c NULL, the string length of the entity is stored in this location.
  478.  *
  479.  * @return A constant string containing the character representation of the given entity.
  480.  */
  481. const char * purple_markup_unescape_entity(const char *text, int *length);
  482.  
  483. /**
  484.  * Returns a newly allocated string containing the value of the CSS property specified
  485.  * in opt. The @a style argument is expected to point to a HTML inline CSS.
  486.  * The function will seek for the CSS property and return its value.
  487.  *
  488.  * For example, purple_markup_get_css_property("direction:rtl;color:#dc4d1b;",
  489.  * "color") would return "#dc4d1b".
  490.  *
  491.  * On error or if the requested property was not found, the function returns
  492.  * @c NULL.
  493.  *
  494.  * @param style A string containing the inline CSS text.
  495.  * @param opt   The requested CSS property.
  496.  *
  497.  * @return The value of the requested CSS property.
  498.  */
  499. char * purple_markup_get_css_property(const gchar *style, const gchar *opt);
  500.  
  501.  
  502. /*@}*/
  503.  
  504.  
  505. /**************************************************************************/
  506. /** @name Path/Filename Functions                                         */
  507. /**************************************************************************/
  508. /*@{*/
  509.  
  510. /**
  511.  * Returns the user's home directory.
  512.  *
  513.  * @return The user's home directory.
  514.  *
  515.  * @see purple_user_dir()
  516.  */
  517. const gchar *purple_home_dir(void);
  518.  
  519. /**
  520.  * Returns the purple settings directory in the user's home directory.
  521.  * This is usually ~/.purple
  522.  *
  523.  * @return The purple settings directory.
  524.  *
  525.  * @see purple_home_dir()
  526.  */
  527. const char *purple_user_dir(void);
  528.  
  529. /**
  530.  * Define a custom purple settings directory, overriding the default (user's home directory/.purple)
  531.  * @param dir The custom settings directory
  532.  */
  533. void purple_util_set_user_dir(const char *dir);
  534.  
  535. /**
  536.  * Builds a complete path from the root, making any directories along
  537.  * the path which do not already exist.
  538.  *
  539.  * @param path The path you wish to create.  Note that it must start
  540.  *        from the root or this function will fail.
  541.  * @param mode Unix-style permissions for this directory.
  542.  *
  543.  * @return 0 for success, nonzero on any error.
  544.  */
  545. int purple_build_dir(const char *path, int mode);
  546.  
  547. /**
  548.  * Write a string of data to a file of the given name in the Purple
  549.  * user directory ($HOME/.purple by default).  The data is typically
  550.  * a serialized version of one of Purple's config files, such as
  551.  * prefs.xml, accounts.xml, etc.  And the string is typically
  552.  * obtained using xmlnode_to_formatted_str.  However, this function
  553.  * should work fine for saving binary files as well.
  554.  *
  555.  * @param filename The basename of the file to write in the purple_user_dir.
  556.  * @param data     A null-terminated string of data to write.
  557.  * @param size     The size of the data to save.  If data is
  558.  *                 null-terminated you can pass in -1.
  559.  *
  560.  * @return TRUE if the file was written successfully.  FALSE otherwise.
  561.  */
  562. gboolean purple_util_write_data_to_file(const char *filename, const char *data,
  563.                                       size_t size);
  564.  
  565. /**
  566.  * Read the contents of a given file and parse the results into an
  567.  * xmlnode tree structure.  This is intended to be used to read
  568.  * Purple's configuration xml files (prefs.xml, pounces.xml, etc.)
  569.  *
  570.  * @param filename    The basename of the file to open in the purple_user_dir.
  571.  * @param description A very short description of the contents of this
  572.  *                    file.  This is used in error messages shown to the
  573.  *                    user when the file can not be opened.  For example,
  574.  *                    "preferences," or "buddy pounces."
  575.  *
  576.  * @return An xmlnode tree of the contents of the given file.  Or NULL, if
  577.  *         the file does not exist or there was an error reading the file.
  578.  */
  579. xmlnode *purple_util_read_xml_from_file(const char *filename,
  580.                                       const char *description);
  581.  
  582. /**
  583.  * Creates a temporary file and returns a file pointer to it.
  584.  *
  585.  * This is like mkstemp(), but returns a file pointer and uses a
  586.  * pre-set template. It uses the semantics of tempnam() for the
  587.  * directory to use and allocates the space for the file path.
  588.  *
  589.  * The caller is responsible for closing the file and removing it when
  590.  * done, as well as freeing the space pointed to by @a path with
  591.  * g_free().
  592.  *
  593.  * @param path   The returned path to the temp file.
  594.  * @param binary Text or binary, for platforms where it matters.
  595.  *
  596.  * @return A file pointer to the temporary file, or @c NULL on failure.
  597.  */
  598. FILE *purple_mkstemp(char **path, gboolean binary);
  599.  
  600. /**
  601.  * Returns an extension corresponding to the image data's file type.
  602.  *
  603.  * @param data A pointer to the image data
  604.  * @param len  The length of the image data
  605.  *
  606.  * @return The appropriate extension, or "icon" if unknown.
  607.  */
  608. const char *
  609. purple_util_get_image_extension(gconstpointer data, size_t len);
  610.  
  611. /*@}*/
  612.  
  613.  
  614. /**************************************************************************/
  615. /** @name Environment Detection Functions                                 */
  616. /**************************************************************************/
  617. /*@{*/
  618.  
  619. /**
  620.  * Checks if the given program name is valid and executable.
  621.  *
  622.  * @param program The file name of the application.
  623.  *
  624.  * @return TRUE if the program is runable.
  625.  */
  626. gboolean purple_program_is_valid(const char *program);
  627.  
  628. /**
  629.  * Check if running GNOME.
  630.  *
  631.  * @return TRUE if running GNOME, FALSE otherwise.
  632.  */
  633. gboolean purple_running_gnome(void);
  634.  
  635. /**
  636.  * Check if running KDE.
  637.  *
  638.  * @return TRUE if running KDE, FALSE otherwise.
  639.  */
  640. gboolean purple_running_kde(void);
  641.  
  642. /**
  643.  * Check if running OS X.
  644.  *
  645.  * @return TRUE if running OS X, FALSE otherwise.
  646.  */
  647. gboolean purple_running_osx(void);
  648.  
  649. /**
  650.  * Returns the IP address from a socket file descriptor.
  651.  *
  652.  * @param fd The socket file descriptor.
  653.  *
  654.  * @return The IP address, or @c NULL on error.
  655.  */
  656. char *purple_fd_get_ip(int fd);
  657.  
  658. /*@}*/
  659.  
  660.  
  661. /**************************************************************************/
  662. /** @name String Functions                                                */
  663. /**************************************************************************/
  664. /*@{*/
  665.  
  666. /**
  667.  * Normalizes a string, so that it is suitable for comparison.
  668.  *
  669.  * The returned string will point to a static buffer, so if the
  670.  * string is intended to be kept long-term, you <i>must</i>
  671.  * g_strdup() it. Also, calling normalize() twice in the same line
  672.  * will lead to problems.
  673.  *
  674.  * @param account  The account the string belongs to, or NULL if you do
  675.  *                 not know the account.  If you use NULL, the string
  676.  *                 will still be normalized, but if the PRPL uses a
  677.  *                 custom normalization function then the string may
  678.  *                 not be normalized correctly.
  679.  * @param str      The string to normalize.
  680.  *
  681.  * @return A pointer to the normalized version stored in a static buffer.
  682.  */
  683. const char *purple_normalize(const PurpleAccount *account, const char *str);
  684.  
  685. /**
  686.  * Normalizes a string, so that it is suitable for comparison.
  687.  *
  688.  * This is one possible implementation for the PRPL callback
  689.  * function "normalize."  It returns a lowercase and UTF-8
  690.  * normalized version of the string.
  691.  *
  692.  * @param account  The account the string belongs to.
  693.  * @param str      The string to normalize.
  694.  *
  695.  * @return A pointer to the normalized version stored in a static buffer.
  696.  */
  697. const char *purple_normalize_nocase(const PurpleAccount *account, const char *str);
  698.  
  699. /**
  700.  * Compares two strings to see if the first contains the second as
  701.  * a proper prefix.
  702.  *
  703.  * @param s  The string to check.
  704.  * @param p  The prefix in question.
  705.  *
  706.  * @return   TRUE if p is a prefix of s, otherwise FALSE.
  707.  */
  708. gboolean purple_str_has_prefix(const char *s, const char *p);
  709.  
  710. /**
  711.  * Compares two strings to see if the second is a proper suffix
  712.  * of the first.
  713.  *
  714.  * @param s  The string to check.
  715.  * @param x  The suffix in question.
  716.  *
  717.  * @return   TRUE if x is a a suffix of s, otherwise FALSE.
  718.  */
  719. gboolean purple_str_has_suffix(const char *s, const char *x);
  720.  
  721. /**
  722.  * Duplicates a string and replaces all newline characters from the
  723.  * source string with HTML linebreaks.
  724.  *
  725.  * @param src The source string.
  726.  *
  727.  * @return The new string.  Must be g_free'd by the caller.
  728.  */
  729. gchar *purple_strdup_withhtml(const gchar *src);
  730.  
  731. /**
  732.  * Ensures that all linefeeds have a matching carriage return.
  733.  *
  734.  * @param str The source string.
  735.  *
  736.  * @return The string with carriage returns.
  737.  */
  738. char *purple_str_add_cr(const char *str);
  739.  
  740. /**
  741.  * Strips all instances of the given character from the
  742.  * given string.  The string is modified in place.  This
  743.  * is useful for stripping new line characters, for example.
  744.  *
  745.  * Example usage:
  746.  * purple_str_strip_char(my_dumb_string, '\n');
  747.  *
  748.  * @param str     The string to strip characters from.
  749.  * @param thechar The character to strip from the given string.
  750.  */
  751. void purple_str_strip_char(char *str, char thechar);
  752.  
  753. /**
  754.  * Given a string, this replaces all instances of one character
  755.  * with another.  This happens inline (the original string IS
  756.  * modified).
  757.  *
  758.  * @param string The string from which to replace stuff.
  759.  * @param delimiter The character you want replaced.
  760.  * @param replacement The character you want inserted in place
  761.  *        of the delimiting character.
  762.  */
  763. void purple_util_chrreplace(char *string, char delimiter,
  764.                           char replacement);
  765.  
  766. /**
  767.  * Given a string, this replaces one substring with another
  768.  * and returns a newly allocated string.
  769.  *
  770.  * @param string The string from which to replace stuff.
  771.  * @param delimiter The substring you want replaced.
  772.  * @param replacement The substring you want inserted in place
  773.  *        of the delimiting substring.
  774.  *
  775.  * @return A new string, after performing the substitution.
  776.  *         free this with g_free().
  777.  */
  778. gchar *purple_strreplace(const char *string, const char *delimiter,
  779.                        const char *replacement);
  780.  
  781.  
  782. /**
  783.  * Given a string, this replaces any utf-8 substrings in that string with
  784.  * the corresponding numerical character reference, and returns a newly
  785.  * allocated string.
  786.  *
  787.  * @param in The string which might contain utf-8 substrings
  788.  *
  789.  * @return A new string, with utf-8 replaced with numerical character
  790.  *         references, free this with g_free()
  791. */
  792. char *purple_utf8_ncr_encode(const char *in);
  793.  
  794.  
  795. /**
  796.  * Given a string, this replaces any numerical character references
  797.  * in that string with the corresponding actual utf-8 substrings,
  798.  * and returns a newly allocated string.
  799.  *
  800.  * @param in The string which might contain numerical character references.
  801.  *
  802.  * @return A new string, with numerical character references
  803.  *         replaced with actual utf-8, free this with g_free().
  804.  */
  805. char *purple_utf8_ncr_decode(const char *in);
  806.  
  807.  
  808. /**
  809.  * Given a string, this replaces one substring with another
  810.  * ignoring case and returns a newly allocated string.
  811.  *
  812.  * @param string The string from which to replace stuff.
  813.  * @param delimiter The substring you want replaced.
  814.  * @param replacement The substring you want inserted in place
  815.  *        of the delimiting substring.
  816.  *
  817.  * @return A new string, after performing the substitution.
  818.  *         free this with g_free().
  819.  */
  820. gchar *purple_strcasereplace(const char *string, const char *delimiter,
  821.                            const char *replacement);
  822.  
  823. /**
  824.  * This is like strstr, except that it ignores ASCII case in
  825.  * searching for the substring.
  826.  *
  827.  * @param haystack The string to search in.
  828.  * @param needle   The substring to find.
  829.  *
  830.  * @return the location of the substring if found, or NULL if not
  831.  */
  832. const char *purple_strcasestr(const char *haystack, const char *needle);
  833.  
  834. /**
  835.  * Returns a string representing a filesize in the appropriate
  836.  * units (MB, KB, GB, etc.)
  837.  *
  838.  * @param size The size
  839.  *
  840.  * @return The string in units form. This must be freed.
  841.  */
  842. char *purple_str_size_to_units(size_t size);
  843.  
  844. /**
  845.  * Converts seconds into a human-readable form.
  846.  *
  847.  * @param sec The seconds.
  848.  *
  849.  * @return A human-readable form, containing days, hours, minutes, and
  850.  *         seconds.
  851.  */
  852. char *purple_str_seconds_to_string(guint sec);
  853.  
  854. /**
  855.  * Converts a binary string into a NUL terminated ascii string,
  856.  * replacing nonascii characters and characters below SPACE (including
  857.  * NUL) into \\xyy, where yy are two hex digits. Also backslashes are
  858.  * changed into two backslashes (\\\\). The returned, newly allocated
  859.  * string can be outputted to the console, and must be g_free()d.
  860.  *
  861.  * @param binary A string of random data, possibly with embedded NULs
  862.  *               and such.
  863.  * @param len The length in bytes of the input string. Must not be 0.
  864.  *
  865.  * @return A newly allocated ASCIIZ string.
  866.  */
  867. char *purple_str_binary_to_ascii(const unsigned char *binary, guint len);
  868. /*@}*/
  869.  
  870.  
  871. /**************************************************************************/
  872. /** @name URI/URL Functions                                               */
  873. /**************************************************************************/
  874. /*@{*/
  875.  
  876. void purple_got_protocol_handler_uri(const char *uri);
  877.  
  878. /**
  879.  * Parses a URL, returning its host, port, file path, username and password.
  880.  *
  881.  * The returned data must be freed.
  882.  *
  883.  * @param url      The URL to parse.
  884.  * @param ret_host The returned host.
  885.  * @param ret_port The returned port.
  886.  * @param ret_path The returned path.
  887.  * @param ret_user The returned username.
  888.  * @param ret_passwd The returned password.
  889.  */
  890. gboolean purple_url_parse(const char *url, char **ret_host, int *ret_port,
  891.                         char **ret_path, char **ret_user, char **ret_passwd);
  892.  
  893. /**
  894.  * This is the signature used for functions that act as the callback
  895.  * to purple_util_fetch_url() or purple_util_fetch_url_request().
  896.  *
  897.  * @param url_data      The same value that was returned when you called
  898.  *                      purple_fetch_url() or purple_fetch_url_request().
  899.  * @param user_data     The user data that your code passed into either
  900.  *                      purple_util_fetch_url() or purple_util_fetch_url_request().
  901.  * @param url_text      This will be NULL on error.  Otherwise this
  902.  *                      will contain the contents of the URL.
  903.  * @param len           0 on error, otherwise this is the length of buf.
  904.  * @param error_message If something went wrong then this will contain
  905.  *                      a descriptive error message, and buf will be
  906.  *                      NULL and len will be 0.
  907.  */
  908. typedef void (*PurpleUtilFetchUrlCallback)(PurpleUtilFetchUrlData *url_data, gpointer user_data, const gchar *url_text, gsize len, const gchar *error_message);
  909.  
  910. /**
  911.  * Fetches the data from a URL, and passes it to a callback function.
  912.  *
  913.  * @param url        The URL.
  914.  * @param full       TRUE if this is the full URL, or FALSE if it's a
  915.  *                   partial URL.
  916.  * @param user_agent The user agent field to use, or NULL.
  917.  * @param http11     TRUE if HTTP/1.1 should be used to download the file.
  918.  * @param cb         The callback function.
  919.  * @param data       The user data to pass to the callback function.
  920.  */
  921. #define purple_util_fetch_url(url, full, user_agent, http11, cb, data) \
  922.     purple_util_fetch_url_request(url, full, user_agent, http11, NULL, \
  923.         FALSE, cb, data);
  924.  
  925. /**
  926.  * Fetches the data from a URL, and passes it to a callback function.
  927.  *
  928.  * @param url        The URL.
  929.  * @param full       TRUE if this is the full URL, or FALSE if it's a
  930.  *                   partial URL.
  931.  * @param user_agent The user agent field to use, or NULL.
  932.  * @param http11     TRUE if HTTP/1.1 should be used to download the file.
  933.  * @param request    A HTTP request to send to the server instead of the
  934.  *                   standard GET
  935.  * @param include_headers
  936.  *                   If TRUE, include the HTTP headers in the response.
  937.  * @param callback   The callback function.
  938.  * @param data       The user data to pass to the callback function.
  939.  */
  940. PurpleUtilFetchUrlData *purple_util_fetch_url_request(const gchar *url,
  941.         gboolean full, const gchar *user_agent, gboolean http11,
  942.         const gchar *request, gboolean include_headers,
  943.         PurpleUtilFetchUrlCallback callback, gpointer data);
  944.  
  945. /**
  946.  * Cancel a pending URL request started with either
  947.  * purple_util_fetch_url_request() or purple_util_fetch_url().
  948.  *
  949.  * @param url_data The data returned when you initiated the URL fetch.
  950.  */
  951. void purple_util_fetch_url_cancel(PurpleUtilFetchUrlData *url_data);
  952.  
  953. /**
  954.  * Decodes a URL into a plain string.
  955.  *
  956.  * This will change hex codes and such to their ascii equivalents.
  957.  *
  958.  * @param str The string to translate.
  959.  *
  960.  * @return The resulting string.
  961.  */
  962. const char *purple_url_decode(const char *str);
  963.  
  964. /**
  965.  * Encodes a URL into an escaped string.
  966.  *
  967.  * This will change non-alphanumeric characters to hex codes.
  968.  *
  969.  * @param str The string to translate.
  970.  *
  971.  * @return The resulting string.
  972.  */
  973. const char *purple_url_encode(const char *str);
  974.  
  975. /**
  976.  * Checks if the given email address is syntactically valid.
  977.  *
  978.  * @param address The email address to validate.
  979.  *
  980.  * @return True if the email address is syntactically correct.
  981.  */
  982. gboolean purple_email_is_valid(const char *address);
  983.  
  984. /**
  985.  * This function extracts a list of URIs from the a "text/uri-list"
  986.  * string.  It was "borrowed" from gnome_uri_list_extract_uris
  987.  *
  988.  * @param uri_list An uri-list in the standard format.
  989.  *
  990.  * @return A GList containing strings allocated with g_malloc
  991.  *         that have been splitted from uri-list.
  992.  */
  993. GList *purple_uri_list_extract_uris(const gchar *uri_list);
  994.  
  995. /**
  996.  * This function extracts a list of filenames from a
  997.  * "text/uri-list" string.  It was "borrowed" from
  998.  * gnome_uri_list_extract_filenames
  999.  *
  1000.  * @param uri_list A uri-list in the standard format.
  1001.  *
  1002.  * @return A GList containing strings allocated with g_malloc that
  1003.  *         contain the filenames in the uri-list. Note that unlike
  1004.  *         purple_uri_list_extract_uris() function, this will discard
  1005.  *         any non-file uri from the result value.
  1006.  */
  1007. GList *purple_uri_list_extract_filenames(const gchar *uri_list);
  1008.  
  1009. /*@}*/
  1010.  
  1011. /**************************************************************************
  1012.  * UTF8 String Functions
  1013.  **************************************************************************/
  1014. /*@{*/
  1015.  
  1016. /**
  1017.  * Attempts to convert a string to UTF-8 from an unknown encoding.
  1018.  *
  1019.  * This function checks the locale and tries sane defaults.
  1020.  *
  1021.  * @param str The source string.
  1022.  *
  1023.  * @return The UTF-8 string, or @c NULL if it could not be converted.
  1024.  */
  1025. gchar *purple_utf8_try_convert(const char *str);
  1026.  
  1027. /**
  1028.  * Salvages the valid UTF-8 characters from a string, replacing any
  1029.  * invalid characters with a filler character (currently hardcoded to
  1030.  * '?').
  1031.  *
  1032.  * @param str The source string.
  1033.  *
  1034.  * @return A valid UTF-8 string.
  1035.  */
  1036. gchar *purple_utf8_salvage(const char *str);
  1037.  
  1038. /**
  1039.  * Compares two UTF-8 strings case-insensitively.  This string is
  1040.  * more expensive than a simple g_utf8_collate() comparison because
  1041.  * it calls g_utf8_casefold() on each string, which allocates new
  1042.  * strings.
  1043.  *
  1044.  * @param a The first string.
  1045.  * @param b The second string.
  1046.  *
  1047.  * @return -1 if @a is less than @a b.
  1048.  *          0 if @a is equal to @a b.
  1049.  *          1 if @a is greater than @a b.
  1050.  */
  1051. int purple_utf8_strcasecmp(const char *a, const char *b);
  1052.  
  1053. /**
  1054.  * Case insensitive search for a word in a string. The needle string
  1055.  * must be contained in the haystack string and not be immediately
  1056.  * preceded or immediately followed by another alpha-numeric character.
  1057.  *
  1058.  * @param haystack The string to search in.
  1059.  * @param needle   The substring to find.
  1060.  *
  1061.  * @return TRUE if haystack has the word, otherwise FALSE
  1062.  */
  1063. gboolean purple_utf8_has_word(const char *haystack, const char *needle);
  1064.  
  1065. /**
  1066.  * Prints a UTF-8 message to the given file stream. The function
  1067.  * tries to convert the UTF-8 message to user's locale. If this
  1068.  * is not possible, the original UTF-8 text will be printed.
  1069.  *
  1070.  * @param filestream The file stream (e.g. STDOUT or STDERR)
  1071.  * @param message    The message to print.
  1072.  */
  1073. void purple_print_utf8_to_console(FILE *filestream, char *message);
  1074.  
  1075. /**
  1076.  * Checks for messages starting with "/me "
  1077.  *
  1078.  * @param message The message to check
  1079.  * @param len     The message length, or -1
  1080.  *
  1081.  * @return TRUE if it starts with /me, and it has been removed, otherwise FALSE
  1082.  */
  1083. gboolean purple_message_meify(char *message, size_t len);
  1084.  
  1085. /**
  1086.  * Removes the underscore characters from a string used identify the mnemonic
  1087.  * character.
  1088.  *
  1089.  * @param in  The string to strip
  1090.  *
  1091.  * @return The stripped string
  1092.  */
  1093. char *purple_text_strip_mnemonic(const char *in);
  1094.  
  1095. /*@}*/
  1096.  
  1097. /**
  1098.  * Adds 8 to something.
  1099.  *
  1100.  * Blame SimGuy.
  1101.  *
  1102.  * @param x The number to add 8 to.
  1103.  *
  1104.  * @return x + 8
  1105.  */
  1106. #define purple_add_eight(x) ((x)+8)
  1107.  
  1108. /**
  1109.  * Does the reverse of purple_escape_filename
  1110.  *
  1111.  * This will change hex codes and such to their ascii equivalents.
  1112.  *
  1113.  * @param str The string to translate.
  1114.  *
  1115.  * @return The resulting string.
  1116.  */
  1117. const char *purple_unescape_filename(const char *str);
  1118.  
  1119. /**
  1120.  * Escapes filesystem-unfriendly characters from a filename
  1121.  *
  1122.  * @param str The string to translate.
  1123.  *
  1124.  * @return The resulting string.
  1125.  */
  1126. const char *purple_escape_filename(const char *str);
  1127.  
  1128. /**
  1129.  * This is added temporarily to assist the split of oscar into aim and icq.
  1130.  * This should not be used by plugins.
  1131.  */
  1132. const char *_purple_oscar_convert(const char *act, const char *protocol);
  1133.  
  1134. /**
  1135.  * Restore default signal handlers for signals which might reasonably have
  1136.  * handlers. This should be called by a fork()'d child process, since child processes
  1137.  * inherit the handlers of the parent.
  1138.  */
  1139. void purple_restore_default_signal_handlers(void);
  1140.  
  1141. #ifdef __cplusplus
  1142. }
  1143. #endif
  1144.  
  1145. #endif /* _PURPLE_UTIL_H_ */
  1146.